! writexml.f90 --
!     Example of writing a simple XML file
!
!     Note:
!     This example uses the fairly low-level routine xml_put.
!     In the near future the xmlreader program should be able
!     to produce a writer routine automatically in much the
!     same way as it can produce a reader routine from the
!     definition.
!
program main

    use xmlparse, only: xml_parse, xml_open, xml_put, xml_close
    implicit none
    type(xml_parse) :: info
    character(len=20) :: tag
    character(len=20), dimension(2, 10) :: attribs
    character(len=50), dimension(10) :: data
    character(len=20) :: type


    ! Open the file for writing (.false.)
    call xml_open(info, "./example/example.xml", .false.)


    ! Write the root element (only the open tag)
    call xml_put(info, "examples", attribs, 0, data, 0, "open")


    ! Write a few elements:
    ! Each element is given with all information that is required, so we
    ! can write the opening and closing tags at once.
    attribs(1, 1) = "name"
    attribs(2, 1) = "tst_menu.f90"
    data(1) = "Define a menubar with nested menus"
    data(2) = "Shows how to deal with arrays"
    call xml_put(info, "example", attribs, 1, data, 2, "elem")

    attribs(1, 1) = "name"
    attribs(2, 1) = "tst_grid.f90"
    attribs(1, 2) = "definition"
    attribs(2, 2) = "grid.xml"
    attribs(1, 3) = "input"
    attribs(2, 3) = "grid_example.xml"
    data(1) = "Conglomerate of options, nothing really structured"
    data(2) = "- despite the name"
    call xml_put(info, "example", attribs, 3, data, 2, "elem")

    ! Close the root tag
    call xml_put(info, "examples", attribs, 0, data, 0, "close")

    ! Close the file
    call xml_close(info)

    write (*, *) "Wrote an XML file: ./example/example.xml"

end program main
